package com.song.service.Impl;

import com.song.dao.ItemsMapper;
import com.song.pojo.Items;
import com.song.service.ItemsService;
import com.song.vo.ItemsVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 宋超
 * HP - the login name of the current user.
 * 2020/12/21 - the current system date.
 * 21:42 - the current system time.
 */
@Service
@Transactional
public class ItemsServiceImpl implements ItemsService {

    @Resource
    private ItemsMapper itemsMapper;

    //查询商品列表
    @Override
    public List<Items> findItemsList(ItemsVo itemsVo) {
        return itemsMapper.findItemsTypeList(itemsVo);
    }

    //添加商品
    @Override
    public int addItems(Items items) {
        items.setReservedNum(0);
        items.setLivedNum(0);
        items.setSurplusNum(items.getLiveNum());
        items.setReservedNum(items.getGoodsNum()-items.getLiveNum());
        return itemsMapper.addItems(items);
    }

    //修改商品
    @Override
    public int updateItems(Items items) {

        //通过id 查询商品信息
        Integer id = items.getId();
        Items itemsById = itemsMapper.findItemsById(id);

        //已购买数量 等于 总量-库存
        items.setLivedNum(itemsById.getGoodsNum()-itemsById.getSurplusNum());
        //库存 等于 总量 - 预订
        //items.setLiveNum(items.getLiveNum()-1);
        return itemsMapper.updateItems(items);
    }

    //删除商品
    @Override
    public int deleteById(int id) {
        return itemsMapper.deleteById(id);
    }

    @Override
    public List<Items> findItemsListByCategoryId() {
        return itemsMapper.findItemsListByCategoryId();
    }

    @Override
    public Items findById(Integer id) {
        return itemsMapper.findById(id);
    }

    @Override
    public List<Items> findByName(String productName) {
        return itemsMapper.findByName(productName);
    }
}
